import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

function getDefaultTipsList() {
    return [
        
        {
            name: '距离',
            id: '3',
            showType:true,
            ClickType: true, //itemList是单选的标记
            itemList: [
                { name: '500m', id: '0.5', isSelect: false },
                { name: '1公里内', id: '1', isSelect: false },
                { name: '2公里内', id: '2', isSelect: false },
                { name: '5公里内', id: '5', isSelect: false },
                { name: '10公里内', id: '10', isSelect: false },
                { name: '不限距离', id: '80000', isSelect: false }
            ]
        },
        {
            name: '价格',
            id: '4',
            showType:true,
            ClickType: true,
            itemList: [
                { name: '1000以内', id: '41', rentStartPrice: '0', rentEndPrice: 1000, isSelect: false },
                { name: '2000以内', id: '42', rentStartPrice: '0', rentEndPrice: 2000, isSelect: false },
                { name: '3000以内', id: '43', rentStartPrice: '0', rentEndPrice: 3000, isSelect: false },
                { name: '自定义', id: '44', type: '价格', rentStartPrice: null, rentEndPrice: null, isSelect: false }
            ]
        },
        {
            name: '房型',
            id: '5',
            showType:true,
            itemList: [
                { name: '1室1厅', id: '51', isSelect: false },
                { name: '2室1厅', id: '52', isSelect: false },
                { name: '3室1厅', id: '53', isSelect: false },
                { name: '3室2厅', id: '54', isSelect: false },
                { name: '4室2厅', id: '55', isSelect: false }
            ]
        },
        {
            name: '状态',
            id: '6',
            showType:true,
            itemList: [
                { name: '空闲', id: '0', isSelect: false },
                { name: '可预定', id: '1', isSelect: false },
            ]
        },
        {
            name: '来源',
            id: '1',
            showType:true,
            itemList: [
                { name: '平台直营', id: '11', isSelect: false },
                { name: '中介挂靠', id: '12', isSelect: false }
            ]
        },
        {
            name: '地标',
            id: '2',
            showType:true,
            itemList: [
                { name: '新城无语', id: '21', isSelect: false },
                { name: '钱岗', id: '22', isSelect: false },
                { name: '紫晶', id: '23', isSelect: false },
                { name: '阳门', id: '24', isSelect: false },
                { name: '绿地都会', id: '25', isSelect: false },
                { name: '巅峰财富', id: '26', isSelect: false }
            ]
        }
    ];
}
const state = {
    tipsList: getDefaultTipsList() // 使用默认函数初始化
};
const mutations = {
    SET_TIPS_LIST(state, newTipsList) {
        state.tipsList = newTipsList;
    },
    RESET_TIPS_LIST(state) {
        state.tipsList = getDefaultTipsList(); // 重置为默认值
    },
    //特定场景，改变某个isSelect的状态
    TOGGLE_ITEM_SELECT(state, { categoryId, itemId, type }) {
        console.log('传进来的参数', categoryId, itemId, type);
        // 找到对应的 category
        const category = state.tipsList.find(cat => cat.id === categoryId);
        if (category) {
            // 检查 ClickType 是否为 true
            if (category.ClickType) {
                // 将所有 itemList 中的 isSelect 设置为 false
                category.itemList.forEach(item => {
                    item.isSelect = false;
                });
            }
            // 找到对应的 item
            const item = category.itemList.find(it => it.id === itemId);
            if (item) {
                // 设置 item 的 isSelect 为传入的 type
                item.isSelect = type;
            }
        } 
    },
    SET_FALSE_SELECT(state) {
        state.tipsList.forEach(tips => {
            tips.itemList.forEach(item => {
                item.isSelect = false; //将全部选项的状态变成false
            });
        });
        console.log('全部状态变成false', state.tipsList);
    }
};

const actions = {
    updateTipsList({ commit }, newTipsList) {
        commit('SET_TIPS_LIST', newTipsList);
    },
    resetTipsList({ commit }) {
        commit('RESET_TIPS_LIST'); // 调用重置 mutation
    },
    toggleItemSelect({ commit }, payload) {
        console.log('传进来的值', payload);
        commit('TOGGLE_ITEM_SELECT', payload); // 调用切换选中状态 mutation
    },
    setisSelectoFalse({ commit }) {
        commit('SET_FALSE_SELECT'); // 调用切换选中状态 mutation
    },
};

export default new Vuex.Store({
    state,
    mutations,
    actions
});
